<template>
	<el-drawer :title="titleMap[mode]" v-model="visible" :size="800" destroy-on-close @closed="$emit('closed')">
		<el-container v-loading="loading">
			<el-main style="padding:0 20px 20px 20px">
				<el-input v-model="form.id" type="hidden"></el-input>
				<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">
					<el-form-item label="归属公司" prop="companyId">
						<el-cascader v-model="form.companyId" :options="companys" :props="groupsProps" @change="handleCompanyChange" :show-all-levels="false" clearable style="width: 100%;"></el-cascader>
					</el-form-item>
					<el-form-item label="归属部门" prop="departmentId">
						<el-cascader v-model="form.departmentId" :options="departments" :props="groupsProps" :show-all-levels="false" clearable style="width: 100%;"></el-cascader>
					</el-form-item>
					<el-form-item label="登录账号" prop="userName">
						<el-input v-model="form.userName" placeholder="请输入" clearable></el-input>
					</el-form-item>
					<el-form-item label="用户姓名" prop="realName">
						<el-input v-model="form.realName" placeholder="请输入" clearable></el-input>
					</el-form-item>
					<el-form-item label="电子邮箱" prop="email">
						<el-input v-model="form.email" placeholder="请输入" clearable></el-input>
					</el-form-item>
					<el-form-item label="手机号码" prop="phone">
						<el-input v-model="form.phone" placeholder="请输入" clearable></el-input>
					</el-form-item>
					<el-form-item label="办公电话" prop="tel">
						<el-input v-model="form.tel" placeholder="请输入" clearable></el-input>
					</el-form-item>
					<el-form-item label="排序" prop="sort">
						<el-input-number v-model="form.sort" controls-position="right" :min="1" ></el-input-number>
					</el-form-item>
					<el-form-item label="性别" prop="sex">
						<el-radio-group v-model="form.sex">
							<el-radio label="1">男</el-radio>
							<el-radio label="2">女</el-radio>
						</el-radio-group>
					</el-form-item>
					<el-form-item label="是否有效" prop="status">
						<el-switch v-model="form.status" :active-value="1" :inactive-value="0"></el-switch>
					</el-form-item>
					<sc-title title="详细信息"></sc-title>
					<el-form-item label="员工工号" prop="userCode">
						<el-input v-model="form.userCode" placeholder="请输入" clearable></el-input>
					</el-form-item>
					<el-form-item label="用户昵称" prop="nickName">
						<el-input v-model="form.nickName" placeholder="请输入" clearable></el-input>
					</el-form-item>
					<el-form-item label="所在岗位" prop="position">
						<el-input v-model="form.position" placeholder="请输入" clearable></el-input>
					</el-form-item>
					<el-form-item label="附属机构" prop="secondaryCompanys">
						<sc-form-table v-model="form.secondaryCompanys" :addTemplate="addSecondaryCompany" drag-sort placeholder="暂无数据">
							<el-table-column prop="companyId" label="附属机构">
								<template #default="scope">
									<el-cascader v-model="scope.row.companyId" :options="companys" :props="groupsProps" @change="handleCompanyChange" :show-all-levels="false" clearable style="width: 100%;"></el-cascader>
								</template>
							</el-table-column>
							<el-table-column prop="position" label="附属岗位">
								<template #default="scope">
									<el-select v-model="scope.row.position" placeholder="请选择岗位">
										<el-option v-for="item in positions" :key="item.value" :label="item.label" :value="item.value"></el-option>
									</el-select>
								</template>
							</el-table-column>
						</sc-form-table>
					</el-form-item>
					<el-form-item label="备注" prop="description">
						<el-input v-model="form.description" clearable type="textarea"></el-input>
					</el-form-item>
					<sc-title title="用户分配角色"></sc-title>
					<el-form-item label="分配角色" prop="roles">
						<sc-table-select v-model="form.roles" :apiObj="apiRoleObj" :table-width="600" multiple clearable :props="roleProps" @change="handleRoleChange" style="width: 100%;">
							<el-table-column prop="id" label="ID" width="180"></el-table-column>
							<el-table-column prop="name" label="角色"></el-table-column>
						</sc-table-select>
					</el-form-item>
				</el-form>
			</el-main>
			<el-footer>
				<el-button type="primary" :loading="isSaveing" @click="submit">保存</el-button>
				<el-button @click="visible=false">取消</el-button>
			</el-footer>
		</el-container>
	</el-drawer>
</template>

<script>
	import { verifyPhone,verifyEmail } from '@/utils/verificate'

	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				apiRoleObj: this.$API.system.role.list,
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id:"",
					parentId: "",
					sort: 1,
					status: "1",
					description: "",
					roles: []
				},
				//验证规则
				rules: {
					sort: [
						{required: true, message: '请输入排序', trigger: 'change'}
					],
					userName: [
						{required: true, message: '请输入登录账号'}
					],
					realName: [
						{required: true, message: '请输入用户姓名'}
					],
					companyId: [
						{required: true, message: '请选择所属机构'}
					],
					departmentId: [
						{required: true, message: '请选择所属部门'}
					],
					phone: [
						{ validator: verifyPhone, trigger:'blur' }
					],
					email: [
						{ validator: verifyEmail, trigger:'blur' }
					],
				},
				//所需数据选项
				companys: [],
				departments: [],
				positions: [],
				roleProps: {
					label: 'name',
					value: 'id'
				},
				groupsProps: {
					value: "id",
					label: "fullName",
					emitPath: false,
					checkStrictly: true
				},
				addSecondaryCompany: {
					companyId: '',
					position: ''
				},
			}
		},
		mounted() {
			this.getGroup()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//加载树数据
			async getGroup(){
				var res = await this.$API.system.company.list.get();
				this.companys = res.data;
			},
			//获取用户角色
			async getRoles(userid){
				var res = await this.$API.system.user.roles.get({userid: userid});
				this.form.roles = res.data;
			},
			async handleCompanyChange() {
				var res = await this.$API.system.dept.list.get({companyId: this.form.companyId});
				this.departments = res.data;
			},
			//值变化
			handleRoleChange(val){
				this.$message('change事件，返回详情查看控制台')
				console.log(val)
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await this.$API.system.user.save.post(this.form);
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.companyId = data.companyId
				this.form.departmentId = data.departmentId
				this.form.userCode = data.userCode
				this.form.userName = data.userName
				this.form.realName = data.realName
				this.form.nickName = data.nickName
				this.form.code = data.code
				this.form.status = data.status
				this.form.sort = data.sort
				this.form.email = data.email
				this.form.phone = data.phone
				this.form.tel = data.tel
				this.form.sex = data.sex
				this.form.position = data.position
				this.form.description = data.description

				if(data.roles == null) {
					this.roles = data.roles
				} else {
					this.getRoles(data.id)
				}


				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.form, data)
			},
			setDefaultCompany(defaultCompany){
			    if(defaultCompany != null) {
					this.form.companyId = defaultCompany.id
			    }
			}
		}
	}
</script>

<style>

</style>
